package com.cloudbroker.openapi.manage.controller;

import static com.cloudbroker.bcs.platform.dav.common.util.RequestUtil.getSingleParameterMapForBody;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloudbroker.bcs.common.entity.HSBCPSBackVO;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.customer.vo.PachSendSMSForm;
import com.cloudbroker.bcs.platform.dav.bizapi.t2service.business.UserManagerT2BizService;
import com.cloudbroker.bcs.platform.dav.common.ext.annotation.SaveLog;

/**
 * 发送中奖短信临时使用的controller
 * @author shenmy14613
 *
 */
@Controller
public class SendMsgController {
    
    @Resource
    private UserManagerT2BizService userManagerT2;
    
    /**
     * 批量发送验证短信
     */
    @ResponseBody
    @RequestMapping("/func_pach_send_sms")
    @SaveLog(remark = "批量发送验证短信")
    public HSBCPSBackVO funcPachSendSms(HttpServletRequest request, String jsonpcallback) {
        PachSendSMSForm form = getSingleParameterMapForBody(request, PachSendSMSForm.class);
        HSBCPSBackVO result = userManagerT2.funcPachSendSms(form);
        if (!VOUtil.isSuccess(result)) {
            return VOUtil.genErrorResultVo(form.getMobiles());
        }
        System.out.println("发送的手机号" + form.getMobiles());
        return VOUtil.genSuccessBackVo();
    }
    
    /**
     * 批量发送验证短信
     */
    @ResponseBody
    @RequestMapping("/func_pach_send_sms_fordafu")
    @SaveLog(remark = "批量发送验证短信")
    public HSBCPSBackVO funcPachSendSmsFordafu(HttpServletRequest request, String jsonpcallback) {
        PachSendSMSForm form = getSingleParameterMapForBody(request, PachSendSMSForm.class);
        form.setContent("各位福星们，大福百万炒股活动已结束，您的最终收益为11元，打开大福小伞微信公众号，回复“收益”点击“领收益”填写您的手机号码及支付宝账号（截止9月30日，过期不候哦），我们将尽快为您发放现金收益！");
        //        File mobilesFile = new File(this.getClass().getResource("/tmp/tmp").getPath());
        InputStream is = this.getClass().getResourceAsStream("/tmp/tmp");
        BufferedReader br = null;
        InputStreamReader reader = null;
        try {
            reader = new InputStreamReader(is);
            br = new BufferedReader(reader);
            String tempString = null;
            int i = 0;
            while ((tempString = br.readLine()) != null) {
                form.setMobiles(tempString);
                HSBCPSBackVO result = userManagerT2.funcPachSendSms(form);
                if (!VOUtil.isSuccess(result)) {
                    return VOUtil.genErrorResultVo(form.getMobiles());
                }
                i++;
                System.out.println("发送第" + i + "条短信：" + tempString);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return VOUtil.genSuccessBackVo();
    }
}
